<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新建会员/车辆信息</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${layerPath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>
	
	<script type="text/javascript">
	var myHeight = 0;
	$(function() {
		//选中后该行高亮
		$('#dataTable tr').bind('click', function() {
			if (window.cur)
				window.cur.style.background = "";
			if (window.cur)
				window.cur.style.fontWeight = "normal";
			this.style.background = "#e8e8e8";
			this.style.fontWeight = "bold";
			window.cur = this;
			$(this).find("td input[type='radio']").attr("checked", true);
			$(this).find("td input[type='radio']").click();
		});
	});

	function rdoChange(id, phone, name, carplate) {
		//查询该会员是否存在套餐
		$("#idHtxt").val(id);
		$("#mobilePhoneHtxt").val(phone);
		$("#memberNameHtxt").val(name);
		$("#carPlateHtxt").val(carplate);
	}
</script>
</head>
<script type="text/javascript">
	$(function() {

		$("#create_member").validation({
			icon : true
		});
		//前台校验框架提交表格之后的处理方法
		$("#saveAndSubmit")
				.click(
						function() {
							if ($("#create_member").valid(this) == false) {
								return false;
							}
							jConfirm(
									"确认保存并提交此记录？",
									"确认提示",
									function(result) {
										if (result == true) {

											$
													.ajax({
														type : 'POST',
														url : '${webcontext}/crmdp/dealer/member/checkMemberExisted',
														async : false,
														data : $(
																'#create_member')
																.serialize(),
														dataType : 'json',
														success : function(data) {
															if (data) {
																if (!data.mobilePhone
																		&& data.carPlate) {
																	//新客户+老车， 提示用户车辆已关联其他客户，是否再关联当前客户
																	jAlert(
																			"此车牌 "
																					+ data.carPlate
																					+ " 已关联其他客户，请重新输入车牌号",
																			"确认提示")
																} else if (!data.mobilePhone
																		&& !data.carPlate) {
																	//新客户+新车
																	$
																			.ajax({
																				type : 'POST',
																				url : '${webcontext}/crmdp/dealer/member/submitToSaveMember',
																				async : false,
																				data : $(
																						'#create_member')
																						.serialize(),
																				dataType : 'json',
																				success : function(
																						data) {
																					if (data.mobilePhone
																							&& data.carPlate) {
																						jAlert(
																								"会员已创建:姓名："
																										+ data.memberName
																										+ ",电话："
																										+ data.mobilePhone
																										+ ",车牌："
																										+ data.carPlate,
																								"提示",
																								function(
																										result) {
																									//window.location.href="${webcontext}/crmdp/dealer/member/getMaintainMemberPage";
																									//创建成功返回
																									parent.location.href = "${webcontext}/crmdp/dealer/member/getMaintainMemberPage";
																								});
																					} else if (data.memberName) {
																						jAlert(
																								"会员 "
																										+ data.memberName
																										+ " 已存在。",
																								"提示",
																								function(
																										result) {
																									//	window.location.href="${webcontext}/crmdp/dealer/member/getMaintainMemberPage";
																								});
																					} else {
																						jAlert(
																								"出错了。",
																								"提示");
																					}

																				}
																			});
																} else {
																	jConfirm(
																			"用户已存在，请使用“会员修改”",
																			"确认提示",
																			function(
																					result) {
																				if (result == true) {
																					parent.location.href = "${webcontext}/crmdp/dealer/member/getMaintainMemberPage";
																				}
																			})
																}
															}
														}
													});
										}
									});

						});

		$("#mobilePhone").focus(function() {
			$('#errorSpan').html("");
		});

		$("#mobilePhone").blur(function() {
			$.ajax({
				type : 'POST',
				url : '${webcontext}/crmdp/dealer/member/checkMemberMobile',
				async : false,
				data : $('#create_member').serialize(),
				dataType : 'json',
				success : function(data) {
					if (data) {
						$("#tell").addClass("has-error");
						$('#mobilePhone_valierr').html("该手机号码已存在");
					} else {
						$('#mobilePhone_valierr').html("");
					}
				}
			});
		});

		$("#carPlate").blur(function() {
			//校验车牌是否符合规范
			$.ajax({
				type : 'POST',
				url : '${webcontext}/crmdp/dealer/member/checkCarPlateRules',
				async : false,
				data : $('#create_member').serialize(),
				dataType : 'json',
				success : function(data) {
					if(data){
						//校验车牌是否存在
						$.ajax({
							type : 'POST',
							url : '${webcontext}/crmdp/dealer/member/checkCarPlate',
							async : false,
							data : $('#create_member').serialize(),
							dataType : 'json',
							success : function(data) {
								if (data) {
									$("#chepai").addClass("has-error");
									$('#carPlate_valierr').html("该车牌号码已存在");
								} else {
									$('#carPlate_valierr').html("");
								}
							}
						});
					}else {
						$("#chepai").addClass("has-error");
						$('#carPlate_valierr').html("该车牌不符合规范");
					}
				}
			});
		});
	});
</script>

<style>
.labelShow {
	float: left;
	width: 20%;
	line-height: 30px;
	text-align: right;
}

.inputDiv {
	float: left;
	width: 50%;
}

.main {
	position: relative;
	margin: 0 auto;
	padding: 10px 10px;
	width: 570px;
	height: 350px;
}
</style>
<body>
	<div class="main">
		<form:form class="form-horizontal" id="create_member" method="post"
			action="" modelAttribute="submitDto" role="form"
			enctype="multipart/form-data">
			<div id="tell" class="form-group has-feedback">
				<label for="mobilePhone" class="col-sm-2 control-label labelShow">手机</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control" id="mobilePhone" name="mobilePhone"
						check-type="mobile">
				</div>
				<span id="mobilePhone_valierr" class="help-block"></span>
			</div>
			<div class="form-group">
				<label for="memberName" class="col-sm-2 control-label labelShow">姓名</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control" id="memberName"
						name="memberName" check-type="required" required-message="请填写姓名" />
				</div>
			</div>
			<div id="chepai" class="form-group has-feedback">
				<label for="carPlate" class="col-sm-2 control-label labelShow">车牌</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control" id="carPlate" name="carPlate"
						check-type="required" required-message="请填写车牌">
				</div>
				<span id="carPlate_valierr" class="help-block"></span>
			</div>
			<div class="form-group text-center" style="margin-left: -100px;">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="button" class="btn btn-success" id="saveAndSubmit">
						<i class="glyphicon glyphicon-ok" style="color: #FFFFFF"></i>保存
					</button>
<!-- 					<button type="button" class="btn btn-success" -->
<!-- 						onClick="closeLayer()"> -->
<!-- 						<i class="glyphicon glyphicon-share-alt" style="color: #FFFFFF"></i>取消 -->
<!-- 					</button> -->
				</div>
			</div>
		</form:form>
	</div>
</body>
</html>